home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d3 / rettig.arc / TRSOURCE.EXE / _TR_GETE.ASM < prev    next >
Assembly Source File  |  1990-10-22  |  5KB  |  142 lines

  1. ;  _TR_GETE.ASM
  2. ;
  3. ;  by Ralph Davis
  4. ;  modified by Leonard Zerman
  5. ;
  6. ; Placed in the public domain by Tom Rettig Associates, 10/22/1990.
  7. ;
  8.  
  9.          PUBLIC   __TR_GETENV
  10.          
  11.          EXTRN    __PSP:WORD     ; C global variable keeps track
  12.                                  ;   of the program segment prefix
  13.                                  ; We'll use this if user has
  14.                                  ;   DOS 2.x
  15.                                  ; Otherwise, we go generic and use
  16.                                  ;   DOS function 62H
  17.  
  18. ;===================
  19. ;
  20. IN_PARMS STRUC
  21.  
  22. OLD_BP   DW       ?
  23.          DD       ?              ; Return address
  24. ENV_STR  DD       ?              ; Pointer to requested environment variable
  25.  
  26. IN_PARMS ENDS
  27. ;
  28. ;===================
  29.  
  30.  
  31.  
  32. ;*****************************************
  33. _TR_GETE_TEXT SEGMENT  BYTE PUBLIC 'CODE'
  34.          ASSUME   CS:_TR_GETE_TEXT
  35. ;-----------------------------------------
  36. ;
  37. ;    __tr_getenv( environment_string);
  38. ;
  39. ;    char *_tr_getenv();
  40. ;       char *environment_string;
  41. ;
  42. ;    Returns:  pointer to requested environment variable
  43. ;
  44. ;----------
  45. __TR_GETENV PROC   FAR
  46.          JMP      SHORT GO
  47.  
  48. ;
  49. ENV_STR_LEN  DW   ?          ; Length of environmental variable requested
  50. ;
  51.  
  52. GO:      PUSH     BP         ; We'll probably use everybody
  53.          MOV      BP,SP      ;  and his Uncle Willie
  54.          PUSH     DS
  55.          PUSH     ES
  56.          PUSH     CX
  57.          PUSH     SI
  58.          PUSH     DI
  59.          MOV      AH,30H     ; Get user's DOS version
  60.          INT      21H
  61.          CMP      AL,3               ; 3.0 or better?
  62.          JGE      _TR_GETENV_COOLDOS
  63.          MOV      AX,__PSP
  64.          MOV      DS,AX              ; and now in DS
  65.          XOR      SI,SI
  66.          JMP      SHORT _TR_GETENV2
  67. ;
  68. _TR_GETENV_COOLDOS:
  69.          MOV      AH,62H             ; Get PSP address
  70.          INT      21H                ; Returns segment address in BX
  71.          MOV      DS,BX
  72.          XOR      SI,SI              ; DS:SI now points to PSP
  73. ;
  74. _TR_GETENV2:
  75.  
  76. ; DS:SI is pointing to the program segment prefix
  77. ; Segment address of the environment string is at offset 2CH
  78. ;   so we add 2CH to SI.
  79.        
  80.          ADD      SI,2CH           
  81.          MOV      AX,[SI]            ; Pick up segment of env string
  82.          MOV      ES,AX
  83.          XOR      DI,DI              ; ES:DI now points to environment string
  84.          MOV      CS:ENV_STR_LEN,DI  ; Initialize length counter
  85.          LDS      SI,[BP].ENV_STR    ; Get address of requested variable
  86.          PUSH     SI                 ; Save it
  87.          XOR      AX,AX
  88.  
  89. _TR_GETENV_FINDNULL:
  90.          CMP      [SI],AL            ; Look for null terminator
  91.          JE       _TR_GETENV3
  92.          CMP      BYTE PTR [SI],'a'  ; Do we need to convert to uppercase?
  93.          JL       _TR_GETENV_NEXTCHAR
  94.          CMP      BYTE PTR [SI],'z'
  95.          JG       _TR_GETENV_NEXTCHAR
  96.          AND      BYTE PTR [SI],0DFH ; Convert to uppercase
  97.  
  98. _TR_GETENV_NEXTCHAR:
  99.          INC      CS:ENV_STR_LEN     ; Bump length count
  100.          INC      SI
  101.          JMP      SHORT _TR_GETENV_FINDNULL
  102.  
  103. _TR_GETENV3:
  104.          POP      SI                ; Restore pointer to parameter passed
  105.          CMP      CS:ENV_STR_LEN,0  ; Was a null string passed?
  106.          JZ       _TR_GETENV_ERR
  107.  
  108. _TR_GETENV4:
  109.          MOV      CX,CS:ENV_STR_LEN ; CX has length of passed string
  110.          PUSH     SI                ; save its address
  111. REPE     CMPSB
  112.          POP      SI                ; and restore it for next time
  113.          JNE      _TR_GETENV5       ; not this one
  114.          INC      DI                ; move past equal sign
  115.          MOV      DX,ES             ; segment pointer to AX
  116.          MOV      AX,DI             ; offset to BX
  117.          JMP      SHORT _TR_GETENV_RET
  118.  
  119. _TR_GETENV5:
  120.          MOV      CX,255
  121. REPNE    SCASB                       ; AL still contains 00H
  122.          CMP      BYTE PTR ES:[DI],0 ; are we at the end of environment?
  123.          JNE      _TR_GETENV4
  124.          
  125. _TR_GETENV_ERR:
  126.          XOR      AX,AX              ; Return NULL pointer
  127.          XOR      DX,DX
  128. _TR_GETENV_RET:
  129.          POP      DI
  130.          POP      SI
  131.          POP      CX
  132.          POP      ES
  133.          POP      DS
  134.          POP      BP
  135.          RET
  136. __TR_GETENV ENDP
  137. ;--------------------------------------------------
  138. _TR_GETE_TEXT   ENDS
  139. ;*******************************************************
  140.          END
  141.  
  142.